package com.huluobo.a2204kotlinshopping.detail

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.huluobo.base.bean.CommentBeanResult
import com.huluobo.base.bean.CommonBeanResult
import com.huluobo.base.bean.DetailBean
import com.huluobo.base.bean.DetailBeanResult
import com.huluobo.base.net.Const
import com.huluobo.base.viewmodel.BaseViewModel
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *  Created by LC on 2024/7/24.
 */
class DetailViewModel : BaseViewModel<DetailRepo>() {
    val getDetailSuccess = MutableLiveData<DetailBeanResult>()
    val getDetailFailed = MutableLiveData<String>()

    val getCommentListSuccess = MutableLiveData<CommentBeanResult>()
    val getCommentListFailed = MutableLiveData<String>()

    val addToCartSuccess = MutableLiveData<CommonBeanResult>()
    val addToCartFailed = MutableLiveData<String>()

    fun addToCart(goodsId: Int, count: Int) {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_GOODS_ID] = goodsId
        map[Const.PARAM_COUNT] = count

        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), json)
        repo.addToCart(body, addToCartSuccess, addToCartFailed)
    }

    fun getCommentList() {
        repo.getCommentList(getCommentListSuccess, getCommentListFailed)
    }

    val goodBean = MutableLiveData<DetailBean>()

    fun getGoodsDetail(goodId: Int) {
        repo.getGoodsDetail(goodId, getDetailSuccess, getDetailFailed)
    }

    override fun createRepo(): DetailRepo = DetailRepo()
}